package dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.OrderDetailDao;
import entity.Easybuy_order_detail;
import entity.Easybuy_product;
import utils.PageUtils;

public class OrderDetailDaoImpl extends BaseDao implements OrderDetailDao{
	
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	@Override
	public int add(Easybuy_order_detail detail) {
		int result=0;
    	try {
    		
    		conn = super.getConnection();
    		
    		String sql=" insert into easybuy_order_detail(orderId,productId,quantity,cost) values(?,?,?,?) ";
    		
    		pstmt = conn.prepareStatement(sql);
    		
    		pstmt.setObject(1, detail.getOrderId());
    		pstmt.setObject(2, detail.getProductId());
    		pstmt.setObject(3, detail.getQuantity());
    		pstmt.setObject(4, detail.getCost());

    		result = pstmt.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	super.closeAll(conn, pstmt, rs);
        }
    	return result;
	}

	@Override
	public PageUtils<Easybuy_order_detail> orderList(int orderid,int pageindex,int pageSize) {
		List<Easybuy_order_detail> list=new ArrayList<Easybuy_order_detail>();
		PageUtils<Easybuy_order_detail> utils=new PageUtils<>();
		utils.setPagesize(pageSize);
		try {
    		
    		conn = super.getConnection();
    		
    		String sql=" select * from easybuy_order_detail where orderId=? limit ?,?";
    		
    		pstmt = conn.prepareStatement(sql);
    		
    		pstmt.setObject(1,orderid);
    		pstmt.setInt(2, pageindex);
			pstmt.setInt(3, pageSize);
			
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_order_detail order=new Easybuy_order_detail();
				order.setId(rs.getInt("id"));
				order.setCost(rs.getDouble("cost"));
				order.setOrderId(rs.getInt("orderId"));
				order.setProductId(rs.getInt("productId"));
				order.setQuantity(rs.getInt("quantity"));
				list.add(order);
			}
			
			utils.setList(list);
			int total=0;
			String sqlCount="select count(1) from easybuy_order_detail where orderId=?";
			
			pstmt=conn.prepareStatement(sqlCount);
			pstmt.setInt(1, orderid);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				total=rs.getInt(1);
			}
			
			utils.setPagetotal(total);
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	super.closeAll(conn, pstmt, rs);
        }
		return utils;
	}

	@Override
	public PageUtils<Easybuy_order_detail> orderdetailList(int pageindex,int pageSize) {
		List<Easybuy_order_detail> list=new ArrayList<Easybuy_order_detail>();
		PageUtils<Easybuy_order_detail> utils=new PageUtils<>();
		utils.setPagesize(pageSize);
		try {
    		
    		conn = super.getConnection();
    		
    		String sql=" select * from easybuy_order_detail  limit ?,?";
    		
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setInt(1, pageindex);
			pstmt.setInt(2, pageSize);
			
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_order_detail order=new Easybuy_order_detail();
				order.setId(rs.getInt("id"));
				order.setCost(rs.getDouble("cost"));
				order.setOrderId(rs.getInt("orderId"));
				order.setProductId(rs.getInt("productId"));
				order.setQuantity(rs.getInt("quantity"));
				list.add(order);
			}
			
			utils.setList(list);
			int total=0;
			String sqlCount="select count(1) from easybuy_order_detail";
			
			pstmt=conn.prepareStatement(sqlCount);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				total=rs.getInt(1);
			}
			
			utils.setPagetotal(total);
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	super.closeAll(conn, pstmt, rs);
        }
		return utils;
	}

}
